package com.train.mgr.modules.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.train.common.exception.BizException;
import com.train.common.model.PageParam;
import com.train.common.model.PageResult;
import com.train.common.utils.AuthUtils;
import com.train.mgr.modules.biz.dto.request.LessonRoleReqDTO;
import com.train.mgr.modules.biz.entity.LessonRole;
import com.train.mgr.modules.biz.mapper.LessonRoleMapper;
import com.train.mgr.modules.biz.service.ILessonRoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课时角色表 服务实现类
 * </p>
 *
 * @author cxg
 * @date 2025-09-30
 */
@Slf4j
@RequiredArgsConstructor
@Service("ILessonRoleService")
public class LessonRoleServiceImpl extends ServiceImpl<LessonRoleMapper, LessonRole> implements ILessonRoleService {

    @Override
    public PageResult<LessonRole> page(PageParam params){
        LambdaQueryWrapper<LessonRole> queryWrapper = Wrappers.lambdaQuery();
        Page<LessonRole> page = baseMapper.selectPage(params.buildPage(), queryWrapper);
        return PageResult.of(page, LessonRole.class);
    }

    @Override
    public boolean add(LessonRoleReqDTO body){
        Long customerId = AuthUtils.getCurrentUser().getCustomerId();
        this.checkByRoleName(body.getRoleName(), body.getId());

        LessonRole bean = BeanUtil.copyProperties(body, LessonRole.class);
        bean.setCustomerId(customerId);
        int rows = baseMapper.insert(bean);
        boolean bool = rows >0 ? true : false;
        return bool;
    }

    @Override
    public boolean update(LessonRoleReqDTO body){
        LessonRole lessonRoleBean = baseMapper.selectById(body.getId());
        if(ObjectUtil.isEmpty(lessonRoleBean)){
            throw new BizException("角色不存在");
        }
        this.checkByRoleName(body.getRoleName(), body.getId());
        BeanUtil.copyProperties(body, lessonRoleBean);
        int rows = baseMapper.updateById(lessonRoleBean);
        boolean bool = rows >0 ? true : false;
        return bool;
    }

    /**
     * 新建编辑公用
     * @param roleName
     * @param id
     */
    private void checkByRoleName(String roleName,Long id) {
        if (StrUtil.isEmpty(roleName)) {
            throw new BizException("角色名称不能为空");
        }
        LambdaQueryWrapper<LessonRole> wrapper = Wrappers.<LessonRole>lambdaQuery()
                .eq(LessonRole::getRoleName, roleName)
                .ne(id != null && id > 0, LessonRole::getId, id) // 如果是编辑，则排除自己
                .eq(LessonRole::getIsDeleted, 0);
        Long result = baseMapper.selectCount(wrapper);
        if(result >0){
            throw new BizException("角色名称重复，请重新输入");
        }
    }

}
